//
//  KSActionSheet.m
//  INMOTION
//
//  Created by 孙凯旋 on 14-9-15.
//  Copyright (c) 2014年 Inmotion. All rights reserved.
//

#import "KSActionSheet.h"
#import "UIView+TapGestureRecognizer.h"
#import "KSLanguageManager.h"

@implementation KSActionSheet

- (id)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (id)initWithContentView:(UIView *)view andStyle:(KSActionStyle)style {
    self = [super init];
    if (self) {
        self.autoresizesSubviews = NO;
        self.frame = [UIApplication sharedApplication].delegate.window.rootViewController.view.bounds;

        self.backgroundView = [[UIView alloc] initWithFrame:self.frame];
        self.backgroundView.backgroundColor = [UIColor blackColor];
        self.backgroundView.alpha = 0.0;
        [self.backgroundView addTapGestureRecognizerWithTarget:self andSelector:@selector(backgroundViewTapped)];
        [self addSubview:self.backgroundView];

        CGFloat y = 0;
        if (style == KSActionStyleWithToolbar) {
            y = 44;

            self.toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 44)];
            self.confirmButton = [[UIBarButtonItem alloc] initWithTitle:IMTNewCarLocalizedString(@"activity_change_bt_pwd_btn_text") style:UIBarButtonItemStyleBordered target:self action:@selector(confirmButtonPressed:)];
            self.confirmButton.tintColor = [UIColor darkGrayColor];
            UIBarButtonItem *spaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
            self.toolbar.items = @[spaceItem, self.confirmButton];
        }

        self.contentView = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height - y - view.frame.size.height, self.frame.size.width, y + view.frame.size.height)];

        view.frame = CGRectMake(view.frame.origin.x, y, view.frame.size.width, view.frame.size.height);

        [self.contentView addSubview:self.toolbar];
        [self.contentView addSubview:view];
        [self addSubview:self.contentView];
    }
    return self;
}

- (void)backgroundViewTapped {
    [self closeView];
}

- (void)closeView {
    [UIView animateWithDuration:0.3 animations:^{
        self.backgroundView.alpha = 0;
        self.contentView.frame = CGRectMake(0, self.frame.size.height, self.contentView.frame.size.width, self.contentView.frame.size.height);
    }];
    [self performSelector:@selector(removeView) withObject:nil afterDelay:0.3];
}

- (void)removeView {
    [self removeFromSuperview];

    if ([self.delegate respondsToSelector:@selector(ksActionSheetDidDismiss:)]) {
        [self.delegate ksActionSheetDidDismiss:self];
    }
}

- (void)show {
    [[UIApplication sharedApplication].delegate.window addSubview:self];

    self.contentView.frame = CGRectMake(0, self.frame.size.height, self.contentView.frame.size.width, self.contentView.frame.size.height);

    [UIView animateWithDuration:0.3 animations:^{
        self.backgroundView.alpha = 0.3;
        NSLog(@"%@", [NSValue valueWithCGRect:self.contentView.frame]);
        self.contentView.frame = CGRectMake(0, self.frame.size.height - self.contentView.frame.size.height, self.contentView.frame.size.width, self.contentView.frame.size.height);
        NSLog(@"%@", [NSValue valueWithCGRect:self.frame]);
    }];
}

- (void)confirmButtonPressed:(UIBarButtonItem *)barButton {
    if ([self.delegate respondsToSelector:@selector(ksActionSheetDidConfirm:)]) {
        [self.delegate ksActionSheetDidConfirm:self];
    }
    [self closeView];
}

@end
